<?php
/**
 * Created by PhpStorm.
 * User: l
 * Date: 2016/7/11
 * Time: 13:35
 */
use app\assets\AppAsset;
use yii\helpers\Html;

AppAsset::register($this);

$baseUrl = Yii::$app->request->baseUrl;
AppAsset::addCss($this, $baseUrl . "/css/shop-bag.css");
$this->title = '我的订单';

$type = $status;
?>
<div style="margin-top: 1px;">
    <div style="width:20%;float:left" onClick=myClick1(9)>
        <div id="divText9" class="titleStyle">
            全部
        </div>
        <div id="underline9" style="background:#ffffff;height:2px">

        </div>
    </div>

    <div style="width:20%;float:left" onClick=myClick1(0)>
        <div id="divText0" class="titleStyle">
            待付款
        </div>
        <div id="underline0" style="background:#ffffff;height:2px">

        </div>
    </div>

    <div style="width:20%;float:left;" onClick=myClick1(1)>
        <div id="divText1" class="titleStyle">
            待发货
        </div>
        <div id="underline1" style="background:#ffffff;height:2px">

        </div>
    </div>

    <div style="width:20%;float:left;" onClick=myClick1(4)>
        <div id="divText4" class="titleStyle">
            待收货
        </div>
        <div id="underline4" style="background:#ffffff;height:2px">

        </div>
    </div>

    <div style="width:20%;float:left;" onClick=myClick1(2)>
        <div id="divText2" class="titleStyle">
            售后
        </div>
        <div id="underline2" style="background:#ffffff;height:2px">

        </div>
    </div>

    <div style="clear: both"></div>
</div>

<div style="width:100% ;height: 1px;background: #e5e5e5;"></div>

<div style="padding-left: 15px;padding-right: 15px" id="shop_note" data-role="page">
    <?php for ($i = 0; $i < count($orders); $i++) { ?>
        <section class="gwc_section">

            <div class="product_title">
                <table width="100%" cellpadding="0" cellspacing="0">
                    <tr>
                        <td width="40px"><img class="shop_icon" src="<?= $baseUrl ?>/images/home_icon.png"/></td>
                        <td><span><?=$shopName?></span></td>
                        <?php if ($orders[$i]['status'] == 0) { ?>
                            <td align="right"><span style="color: #FFA500">等待买家付款</span></td>
                        <?php } else if ($orders[$i]['status'] == 1) { ?>
                            <td align="right"><span style="color: #FFA500">等待卖家发货</span></td>
                        <?php } else if ($orders[$i]['status'] == 2) { ?>
                            <td align="right"><span style="color: #FFA500">退款中</span></td>
                        <?php } else if ($orders[$i]['status'] == 3) { ?>
                            <td align="right"><span style="color: #FFA500">已退款</span></td>
                        <?php } else if ($orders[$i]['status'] == 4) { ?>
                            <td align="right"><span style="color: #FFA500">等待买家收货</span></td>
                        <?php } else if ($orders[$i]['status'] == 5) { ?>
                            <td align="right"><span style="color: #FFA500">退货中</span></td>
                        <?php } else if ($orders[$i]['status'] == 6) { ?>
                            <td align="right"><span style="color: #FFA500">已退货</span></td>
                        <?php } else if ($orders[$i]['status'] == 7) { ?>
                            <?php if($orders[$i]['is_comment'] == 0){?>
                                <td align="right"><span style="color: #FFA500">交易完成</span></td>
                            <?php } else {?>
                                <td align="right"><span style="color: #FFA500">已评价</span></td>
                            <?php }?>

                        <?php } else if ($orders[$i]['status'] == 8) { ?>
                            <td align="right"><span style="color: #FFA500">已关闭</span></td>
                        <?php } ?>
                    </tr>
                </table>
            </div>

            <?php for ($j = 0; $j < count($commodityInOrders[$i]); $j++) { ?>
                <div class="product_hover">

                    <table width="100%" cellpadding="0" cellspacing="0">
                        <tr>
                            <td width="90px"><div class="image-div"><img class="product_icon"
                                                                         src="<?= $commodityInOrders[$i][$j]['pic_url'] ?>"/></div></td>

                            <td style="padding-left:10px;line-height: 100%;padding-top: 6px">
                                <p style="height: 30px;overflow: hidden;"><?= $commodityInOrders[$i][$j]['commodity_name'] ?></p>

                                <p><?php for ($n = 0; $n < count($commodityAttrsInOrders[$i]); $n++) { ?>
                                        <?= $commodityAttrsInOrders[$i][$n]['attr_opt_name'] ?>
                                    <?php } ?>
                                </p>

                                <div>
                                    <p style="color: #FFA500;float: left;">
                                        ￥<?= $commodityInOrders[$i][$j]['price'] ?></p>

                                    <p style="float: right">
                                        ×<?= $orderCommodities[$i][$j]['commodity_num']; ?>
                                    </p>
                                </div>
                            </td>
                        </tr>
                    </table>
                </div>
            <?php } ?>

            <div class="product_bottom">
                <table width="100%" cellpadding="0" cellspacing="0">
                    <tr>
                        <td><span style="vertical-align: middle" class="txtred">合计：<?= floatval($orders[$i]['money']) + floatval($orders[$i]['postage']) ?> (含运费：<?= floatval($orders[$i]['postage']) ?>)</span>
                        </td>

                        <?php if ($orders[$i]['status'] == 0) {

                                    $commodity_id = array();
                                    $commodity_num = array();
                                    for ($j = 0; $j < count($commodityInOrders[$i]); $j++) {
                                        array_push($commodity_id, $orderCommodities[$i][$j]['commodity_id']);
                                        array_push($commodity_num, $orderCommodities[$i][$j]['commodity_num']);
                                    }
                                    $commodity_id = json_encode($commodity_id);
                                    $commodity_num = json_encode($commodity_num);?>
                                <td align="right" width="40">
                                    <div class="pay_myorder" onclick='closeOrder(8 , <?= $orders[$i]['orderform_id']?>,<?= $commodity_id ?>,<?= $commodity_num ?>)'>
                                        关闭
                                    </div>
                            </td>

                            <td align="right" width="50" style="padding-left: 10px">
                                <div class="pay_myorder" onclick="payMoney(<?= $orders[$i]['money'] ?>,<?= $orders[$i]['orderform_id']?>)">
                                    付款
                                </div>
                            </td>

                        <?php } else if ($orders[$i]['status'] == 1) { ?>

                            <td align="right" width="40">
                                <div class="pay_myorder" onclick="changeStatus(2 , <?= $orders[$i]['orderform_id']?>)">
                                    退款
                                </div>
                            </td>

                        <?php } else if ($orders[$i]['status'] == 4) { ?>

                            <td align="right" width="80">
                                <div class="pay_myorder" style="text-decoration:none" onclick="viewLogistics(<?= $orders[$i]['orderform_id']?>)">查看物流</div>
                            </td>

                            <td align="right" width="50" style="padding-left: 10px">
                                <div class="pay_myorder" onclick='changeStatus(5 , <?= $orders[$i]['orderform_id']?>)'>
                                    退货
                                </div>
                            </td>

                            <td align="right" width="90" style="padding-left: 10px">
                                <div class="pay_myorder" onclick='confirmGet(<?php echo json_encode($orders[$i])?>)'>
                                    确认收货
                                </div>
                            </td>
                        <?php } else if ($orders[$i]['status'] == 7) { ?>

                            <?php if($orders[$i]['is_comment'] == 0){?>

                                <td align="right" width="40">
                                    <div class="pay_myorder" onclick="changeCommodity(0 ,<?= $orders[$i]['orderform_id']?>)">
                                        差评
                                    </div>
                                </td>

                                <td align="right" width="50" style="padding-left: 10px">
                                    <div class="pay_myorder" onclick="changeCommodity(1,<?= $orders[$i]['orderform_id']?>)">
                                        中评
                                    </div>
                                </td>

                                <td align="right"width="50" style="padding-left: 10px">
                                    <div class="pay_myorder" onclick="changeCommodity(2,<?= $orders[$i]['orderform_id']?>)">
                                        好评
                                    </div>
                                </td>
                            <?php }?>
                        <?php } ?>
                    </tr>
                </table>
            </div>
        </section>
    <?php } ?>
</div>

<script type="text/javascript">

    var params="";
    var orderform_id="";

    document.getElementById("divText"+<?= $type?>).style.color = "#ff7614";
    document.getElementById("underline"+<?= $type?>).style.background = "#ff7614";

    $(document).ready(function(){
        function imageLoaded(){
            $(this).attr("hidden",false);
            var height = $(this).css('height');
            var width = $(this).css('width');
            if (parseInt(height.substring(0,height.length-2))>parseInt(width.substring(0,width.length-2))){
                $(this).css('height',"100%");
                $(this).width('auto');
            }else {
                $(this).css('width',"100%")
                $(this).height('auto');
            }
        }
        $(".product_icon").each(function(){
            if(this.complete){
                imageLoaded.call(this);
            }else{
                $(this).one('load', imageLoaded);
            }
        });
    });

    function myClick1(num) {
        if (num == 0) {
            location.href = "my-order?status=0";
        } else if (num == 1) {
            location.href = "my-order?status=1";
        } else if (num == 2) {
            location.href = "my-order?status=2";
        } else if (num == 4) {
            location.href = "my-order?status=4";
        } else if(num == 9){
            location.href = "my-order?status=9";
        }
    }

    function payMoney(total , order_id) {
        $.ajax({
            type:"POST",
            dataType:"json",
            data:{
                "order_id":order_id ,
                "total":total
            },
            url:"pay",
            success:function(json){
//                console.log("123",json);
                params=json.js;
                orderform_id=json.orderform_id;
                callpay(params);
            },
            error:function(data){
//                console.log(data);
                alert("支付失败");
            }
        });
    }

    function callpay(pay_param)
    {
        if (typeof WeixinJSBridge == "undefined"){
            if( document.addEventListener ){
                document.addEventListener('WeixinJSBridgeReady', jsApiCall.bind(this,pay_param), false);
            }else if (document.attachEvent){
                document.attachEvent('WeixinJSBridgeReady', jsApiCall.bind(this,pay_param));
                document.attachEvent('onWeixinJSBridgeReady', jsApiCall.bind(this,pay_param));
            }
        }else{
            jsApiCall(pay_param);
        }
    }

    function jsApiCall(payparam)
    {
        console.log(payparam);
        WeixinJSBridge.invoke(
            'getBrandWCPayRequest',
            payparam,
            function(res){
                if(res.err_msg == "get_brand_wcpay_request:ok"){
                    $.ajax({
                        type:"POST",
                        dataType:"json",
                        data: {
                            "orderform_id": orderform_id

                        },
                        url: "getorderstatus",
                        success: function (json) {
                            if(json.code){
                                console.log('success');
                                location.href="my-order?status=1";
                            }

                        },
                        error: function (XmlHttpRequest,textStatus, errorThrown) {
                            console.log(XmlHttpRequest.responseText);
                            console.log(textStatus);

                            //  location.href = "login";
                        }
                    });

                }else if(res.err_msg == "get_brand_wcpay_request:cancel")
                {

                    alert('支付过程中用户取消！');
                    return;

                }else{

                    alert('支付失败！');
                    return;
                }
                //WeixinJSBridge.log(res.err_msg);
                //  console.log(res.err_code+res.err_desc+res.err_msg);
            }
        );
    }

    function confirmGet(orderform) {
        $.ajax({
            type:"POST",
            dataType:"json",
            data: {
                "orderform_id": orderform['orderform_id'],
                "money":orderform['money'],
                "proxy_id":orderform['proxy_id']
            },
            url: "settlement-commission",
            success: function (json) {
                alert("收货成功!");
                location.href = "my-order?status=9";
            },
            error: function (data) {
                alert("收货失败", data);
            }
        });

    }

    function changeStatus(order_status, order_id){
        console.log(order_status);
        console.log(order_id);
        $.ajax({
            type:"POST",
            dataType:"json",
            data: {
                "orderform_id": order_id,
                "status":order_status
            },
            url: "change-status",
            success: function (json) {
                alert("申请成功!");
                location.href = "my-order?status=2";
            },
            error: function (data) {
                alert("申请失败", data);
            }
        });
    }

    function closeOrder(order_status, order_id,commodity_id,commodity_num){
        $.ajax({
            type:"POST",
            dataType:"json",
            data: {
                "orderform_id": order_id,
                "status":order_status,
                'commodity_id':commodity_id,
                'commodity_num':commodity_num,
            },
            url: "change-status",
            success: function (json) {
                alert("关闭成功!");
                location.href = "my-order?status=9";
            },
            error: function (data) {
                alert("关闭失败", data);
            }
        });
    }

    function changeCommodity(comment_rank,order_id){
        $.ajax({
            type:"POST",
            dataType:"json",
            data: {
                "orderform_id": order_id,
                "comment_rank":comment_rank
            },
            url: "comment-order",
            success: function (json) {
                alert("评论成功!");
                location.href = "my-order?status=9";
            },
            error: function (data) {
                alert("评论失败", data);
            }
        });
    }


    function viewLogistics(orderform_id){
        console.log('ssss',orderform_id);
        $.ajax({
            type:"POST",
            dataType:"json",
            data: {
                "orderform_id": orderform_id,

            },
            url: "view-logistics",
            success: function (json) {

                newwindow(json)

            },
            error: function (data) {
              alert(data);
            }
        });

    }

    function newwindow(json){
//        var url="http://m.kuaidi100.com/result.jsp?com=kuaijiesudi&nu="+json;
        //window.open(url);
        window.location.href="http://m.kuaidi100.com/result.jsp?nu="+json;

    }


</script>